package com.kefu.common.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;


/**
 * Created by Administrator on 2016/11/29.
 */
public class JedisUtils {
    private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);

    public static String host = "127.0.0.1";
    public  static int port = 6379;

    public static   JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
    public static Jedis jedis = jedisPool.getResource();

    /**
     * 如果此频道没有订阅者,就返回空值。
     */
    public static String pubsubChannels(String pattern){
        logger.info("如果此频道没有订阅者,就返回空List："+jedis.pubsubChannels(pattern));
        return String.valueOf(jedis.pubsubChannels(pattern));

    }
    /**
     * 测试类：如果此频道没有订阅者,就返回空值。
     */
    public static void testpubsubChannels(String pattern){

        logger.info("如果此频道没有订阅者,就返回空List："+jedis.pubsubChannels(pattern));
        if (String.valueOf(jedis.pubsubChannels(pattern))=="[]"){
            System.out.println("空值");
        }else {
            System.out.println("不是空值");
        }
    }

    /**
     * 测试pubsubNumSub方法
     */
    public static void pubsubNumSub(){
        jedis.pubsubNumPat();
        logger.info("这个是返回订阅模式的数量："+String.valueOf(jedis.pubsubNumPat()));
        logger.info("这个是订阅了“firsttest”频道的client数量："+String.valueOf(jedis.pubsubNumSub("firsttest")));
    }


}
